package com.meida.module.product.provider.service.impl;

import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ListModel;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaDelete;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.product.client.entity.ProdCategory;
import com.meida.module.product.client.entity.ProdSku;
import com.meida.module.product.provider.mapper.ProdSkuMapper;
import com.meida.module.product.provider.service.ProdSkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * sku表,该表表示具体的商品实体,如黑色的 64g的iphone 8 实现类
 *
 * @author flyme
 * @date 2019-07-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProdSkuServiceImpl extends BaseServiceImpl<ProdSkuMapper, ProdSku> implements ProdSkuService {

    @Override
    public Boolean deleteByProductId(Long[] productId) {
        CriteriaDelete cd = new CriteriaDelete();
        cd.in(true, "productId", productId);
        baseDelete(cd);
        return true;
    }

    @Override
    public Boolean deleteByProductId(Long productId) {
        CriteriaDelete cd = new CriteriaDelete();
        cd.eq(true, "productId", productId);
        baseDelete(cd);
        return true;
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<EntityMap> selectByProductId(Long productId) {
        CriteriaQuery cq = new CriteriaQuery(ProdSku.class);
        cq.select(ProdSku.class, "skuId", "title", "images", "num", "productNo", "price", "ownSpec", "indexes");
        cq.eq(ProdSku.class, "productId", productId);
        return selectEntityMap(cq);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody listEntityMap(ListModel listModel) {
        CriteriaQuery cq = new CriteriaQuery(listModel, ProdSku.class);
        cq.select(ProdSku.class, "skuId", "title", "images", "num", "productNo", "price");
        cq.eq(ProdSku.class, "productId");
        return baseList(cq);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforeGet(CriteriaQuery<ProdSku> cq, ProdSku prodSku, EntityMap requestMap) {
        if (FlymeUtils.isNotEmpty(prodSku)) {
            cq.eq(true, "productId", prodSku.getProductId());
            cq.eq(true, "indexes", prodSku.getIndexes());
        }
        return ResultBody.ok();
    }

    @Override
    public List<Long> selectIds(Long companyId) {
        CriteriaQuery cq = new CriteriaQuery(ProdCategory.class);
        cq.select("skuId");
        cq.eq(true, "companyId", companyId);
        return listObjs(cq, e -> Long.parseLong(e.toString()));
    }
}
